#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
sys.path.append(os.path.abspath('../..'))
import time
import camfwlib
from ctypes import *
import struct

if __name__ == '__main__':
    path = './images/'

    # shared libray settings
    cfunc = cdll.LoadLibrary('./libcfunc.so')

    # camfwlib settings
    cf = camfwlib.camfwClient('172.16.80.30', 15680, './camfw.log', camfwlib.DEBUG) 
    cam_list = cf.get_cameralist()
    cf.log(camfwlib.DEBUG, 'returned camera id list : ' + ' '.join(cam_list))

    #print "=== start streaming ==="
    cf.start_streaming('cam1')

    for t in range(0,5):
        frames = cf.get_imagelist('cam1', 'last')
        for frame in frames:
            print "output : %s" % frame
            in_image = cf.get_imagedata(frame)

            out_buff = create_string_buffer(50000)
            cfunc.cv_interface.argtypes = [c_char_p, c_int, c_char_p]
            cfunc.cv_interface(in_image, len(in_image), out_buff)

            size = struct.unpack('i', out_buff.raw[0:4])[0]
            if size!=0:
                print "unpack size = %d" % size
                out_image = out_buff.raw[4:size+4]

                fp = open( path + frame + '.png', 'w')
                fp.write(out_image)
                fp.close()

    cf.stop_streaming('cam1')
    #print "=== stop streaming ==="
